package com.fks.fks_service.mapper;

import com.fks.fks_service.pojo.UserDB;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface UserDBMapper {

    @Select("select * from fks_user where phone=#{phone}")
    UserDB selectUserByPhone(String phone);

    @Select("select * from fks_user where phone=#{phone}")
    UserDB selectUserByUsername(String phone);

    @Insert("insert into fks_user(userUniqueIdentification,userName,nickName,phone,createTime,avatar,password" +
            ",directUid,indirectUid,payPassword) " +
            "values (#{userUniqueIdentification},#{userName},#{nickName},#{phone},#{createTime},#{avatar}" +
            ",#{password},#{directUid},#{indirectUid},#{payPassword})")
    int insertUserDB(UserDB registerUserDB);

    @Select("select * from fks_user where id=#{uid}")
    UserDB getUserById(int uid);

    @Update("update fks_user set nickName=#{nickName},realGender=#{realGender},personalSignature=#{personalSignature}" +
            ",avatar=#{avatar} where id=#{userId}")
    int updateUser(int userId, String nickName, String realGender, String personalSignature,String avatar);

    @Update("update fks_user set merchantUserId=#{merchantUserId} where id=#{userId}")
    int bindingMerchantUserId(int merchantUserId, int userId);

    @Update("update fks_user set password=#{password} where id=#{id}")
    int updatePassword(String password, int id);

    @Update("update fks_merchant_user set withdrawalAccount=#{withdrawalAccount},withdrawalAccountName=#{withdrawalAccountName} where id=#{userId}")
    int setWithdrawalAccount(String withdrawalAccount, String withdrawalAccountName, int userId);

    @Update("update fks_user set userName=#{phone},phone=#{phone} where id=#{userId}")
    int updateUserPhone(int userId, String phone);

    @Select("select * from fks_user where userUniqueIdentification=#{userUniqueIdentification}")
    UserDB selectUserByUserUniqueIdentification(String userUniqueIdentification);

    @Update("update fks_user set payPassword=#{payPassword} where id=#{id}")
    int updatePayPassword(UserDB user);

    @Update("update fks_user set realName=#{realName},iDCard=#{iDCard},IdentityInformationVerification='1' where id=#{id}")
    int realNameAuthentication(String realName, String iDCard, int id);



    @Update("update fks_user set identityInformationVerification=#{identityInformationVerification} where id=#{userId}")
    int realNameVerification(String userId, String identityInformationVerification);

    @Select("select id,nickName,phone,avatar,status,identityInformationVerification,realName,iDCard from fks_user where identityInformationVerification=1 limit #{page},#{pageSize}")
    List<UserDB> getRealNameAuthenticationList(int page, int pageSize);

    @Select("select id,nickName,phone,avatar,status,identityInformationVerification,realName" +
            ",iDCard,directUid,indirectUid,integral,accumulatedPoints,deviceNumber,IdentityInformationVerification" +
            " from fks_user limit #{page},#{pageSize}")
    List<UserDB> getUserDetailsList(int page, int pageSize);

    @Select("select count(*) from fks_user where directUid=#{userId}")
    int getUserDirectUserNumber(int userId);

    @Select("select count(*) from fks_user where indirectUid like '%${userId}%'")
    int getUserIndirectUserNumber(String userId);

    @Select("select count(deviceNumber) from fks_user where indirectUid like '%${userId}%'")
    int getTeamDeviceNumber(String userId);

    @Update("update fks_user set integral=#{integral} where id=#{userId}")
    int updateUserIntegral(int userId, String integral);

    @Update("update fks_user set integral=#{integral},promotePoints=#{promotePoints}" +
            ",accumulatedPoints=#{accumulatedPoints} where id=#{id}")
    void updateUserIntegralAndAccumulatedPoints(int id, String integral,String promotePoints , String accumulatedPoints);

    @Update("update fks_user set deviceNumber=#{deviceNumber} where id=#{userId}")
    int updateDeviceNumber(int userId, int deviceNumber);

    @Update("update fks_user set integral=#{integral},directPushPoints=#{directPushPoints}" +
            ",accumulatedPoints=#{accumulatedPoints} where id=#{id}")
    int addDirectUserIntegral(int id, String  integral, String directPushPoints,String accumulatedPoints);

    @Update("update fks_user set deviceNumber=#{deviceNumber} where id=#{uid}")
    int addDeviceNumber(int uid, int deviceNumber);

    @Update("update fks_user set integral=#{integral} where id=#{id}")
    int updateIntegral(int id, String integral);

    @Update("update fks_user set grade=#{grade},yieldRate=#{yieldRate},isVip='1' where id=#{userId}")
    int updateUserGrade(String userId, String grade, String yieldRate);

    @Update("update fks_user set grade=#{grade},yieldRate=#{yieldRate} where id=#{userId}")
    int replaceGrade(int userId, String grade, String yieldRate);

    @Update("update fks_user set integral=#{integral} ,accumulatedPoints=#{accumulatedPoints} ,dailyTasksNumber=#{dailyTasksNumber} where id=#{userId}")
    int dailyTasksSubmit(int userId, String integral, String accumulatedPoints, int dailyTasksNumber);

    @Update("update fks_user set dailyTasksNumber='5'")
    int updateDailyTasksNumber();

    @Select("select * from fks_user kc where (select count(*) from fks_user where directUid = kc.id ) >= 5 " +
            " and (select sum(deviceNumber) from fks_user where directUid = kc.id) >= 10")
    List<UserDB> getOneUser();

    @Select("select * from fks_user kc where (select count(*) from fks_user where directUid = kc.id ) >= 12 " +
            " and (select sum(deviceNumber) from fks_user where directUid = kc.id) >= 30")
    List<UserDB> getTwoUser();

    @Select("select * from fks_user kc where (select count(*) from fks_user where directUid = kc.id ) >= 50 " +
            " and (select sum(deviceNumber) from fks_user where directUid = kc.id) >= 120")
    List<UserDB> getThreeUser();

    @Select("select * from fks_user kc where (select count(*) from fks_user where directUid = kc.id ) >= 80 " +
            " and (select sum(deviceNumber) from fks_user where directUid = kc.id) >= 200")
    List<UserDB> getFourUser();

    @Select("select * from fks_user kc where (select count(*) from fks_user where directUid = kc.id ) >= 120 " +
            " and (select sum(deviceNumber) from fks_user where directUid = kc.id) >= 300")
    List<UserDB> getFiveUser();

    @Update("update fks_user set aliPayAccount=#{aliPayAccount},aliPayRealName=#{aliPayRealName} where id=#{userId}")
    int setAliPayAccount(String userId, String aliPayAccount, String aliPayRealName);

    @Update("update fks_user set integral=#{integral},shopPoints=#{shopPoints},accumulatedPoints=#{accumulatedPoints}" +
            " where id=#{id}")
    int updateIntegralAndShopPoints(int id, String integral, String shopPoints, String accumulatedPoints);

    @Update("update fks_user set deviceNumber=0")
    int quantityResetToZero();

    @Select("select count(*) from fks_user where indirectUid like '%${indirectUid}%'")
    int getTeadNumber(String indirectUid);

    @Select("select count(*) from fks_user where directUid=#{directUid}")
    int getDirectPushNumber(String directUid);

    @Select("select count(*) from fks_user where indirectUid like '%${indirectUid}%' and deviceNumber>0")
    int getVipNumber(String indirectUid);

    @Select("select * from fks_user where directUid=#{directUid}")
    List<UserDB> getDirectPushList(String directUid);

    @Select("select * from fks_user where indirectUid like '%${indirectUid}%' and directUid>#{userId}")
    List<UserDB> getInterPushList(String userId, String indirectUid);

    @Select("select directPushPoints from fks_user where id=#{userId}")
    String getDirectPushPerformance(String userId);

    @Select("select promotePoints from fks_user where id=#{userId}")
    String getInterPushPerformance(String userId);

    @Select("select * from fks_user where phone=#{phone}")
    UserDB getUserByPhone(String phone);

    @Select("select * from fks_user where userUniqueIdentification=#{uniqueIdentification}")
    UserDB getUserByUniqueIdentification(String uniqueIdentification);

    @Update("update fks_user set grade='0' ,yieldRate='0' where id=id")
    int updateAllGrade();

    @Select("select userName,integral from fks_user order by integral desc limit 0,10")
    List<UserDB> getRankingList();

    @Update("update fks_user set qq=#{qq} where id=#{id}")
    int addQQ(UserDB user);

    @Update("update fks_user set wx=#{wx} where id=#{id}")
    int addWX(UserDB user);

    @Select("select count(*) from kc_user ")
    int getUserDetailsCount();
}
